//
//  HUDTools.m
//  CityLife
//
//  Created by 郑佳 on 16/1/21.
//  Copyright © 2016年 Triangle. All rights reserved.
//

#import "HUDTools.h"
#import "MBProgressHUD.h"

static CGFloat const dismissTime = 1.5f;

@implementation HUDTools

+ (void)zj_showLoadingInView:(id)view{
   [HUDTools zj_showLoadingInView:view title:nil];
}

+ (void)zj_showLoadingInView:(UIView *)view title:(NSString *)title{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.label.text = NSLocalizedString(title, @"HUD message title");
        hud.label.font = [UIFont boldSystemFontOfSize:15.0];
        hud.contentColor = [UIColor whiteColor];
        hud.bezelView.backgroundColor = [UIColor colorWithRed:38.0/255.0 green:38.0 / 255.0 blue:38.0/ 255.0 alpha:1];
        hud.removeFromSuperViewOnHide = YES;
        hud.margin = 10;
    });
}

+ (void)zj_showSuccessStatusInView:(UIView *)view title:(NSString *)title{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self zj_hideInView:view];
        MBProgressHUD *hud = [self hudInView:view title:title];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"success"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        [hud hideAnimated:YES afterDelay:dismissTime];
    });
}

+ (void)zj_showSuccessStatusInView:(UIView *)view title:(NSString *)title completeBlock:(void (^)())completeBlock {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self zj_hideInView:view];
        MBProgressHUD *hud = [self hudInView:view title:title];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"success"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        [hud hideAnimated:YES afterDelay:dismissTime];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(dismissTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (completeBlock) {
                completeBlock();
            }
        });
    });
}

+ (void)zj_showInfoStatusInView:(UIView *)view title:(NSString *)title {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self zj_hideInView:view];
        MBProgressHUD *hud = [self hudInView:view title:title];
        hud.mode = MBProgressHUDModeText;
        hud.label.numberOfLines = 0;
//        hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
        [hud hideAnimated:YES afterDelay:dismissTime];
    });    
}

+ (void)zj_hideInView:(UIView *)view{
    [MBProgressHUD hideHUDForView:view animated:YES];
}

+ (MBProgressHUD *)hudInView:(UIView *)view title:(NSString *)title{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = NSLocalizedString(title, @"HUD message title");
    hud.label.font = [UIFont boldSystemFontOfSize:15.0];
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = [UIColor colorWithRed:38.0/255.0 green:38.0 / 255.0 blue:38.0/ 255.0 alpha:1];
    hud.removeFromSuperViewOnHide = YES;
    hud.margin = 10;
    hud.graceTime = 2.0;
    return hud;
}


@end
